package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.TruyenTinhCamDao;
import com.truyen.persistence.entity.TacGia;
import com.truyen.persistence.entity.ChapterTam;
import com.truyen.persistence.entity.ChapterTruyenTinhCam;
import com.truyen.persistence.entity.ChapterTmpTruyenTinhCam;
import com.truyen.persistence.entity.TruyenTinhCam;
import com.truyen.service.ChapterTamService;
import com.truyen.service.ChapterTruyenTinhCamService;
import com.truyen.service.ChapterTmpTruyenTinhCamService;
import com.truyen.service.TruyenTinhCamService;
import com.truyen.utils.PagingInfo;


@Service
public class TruyenTinhCamServiceImpl implements TruyenTinhCamService {
	
	@Autowired
	private TruyenTinhCamDao truyenTinhCamDao;
	
	@Autowired
	private ChapterTruyenTinhCamService chapterTruyenTinhCamService;
	
	@Autowired
	private ChapterTmpTruyenTinhCamService chapterTmpTruyenTinhCamService;
	
	@Autowired
	private ChapterTamService chapterTamService;

	@Override
	public void save(TruyenTinhCam entity) {
		truyenTinhCamDao.save(entity);
	}

	@Override
	public TruyenTinhCam findById(Long id) {
		return truyenTinhCamDao.findById(id);
	}

	@Override
	public List<TruyenTinhCam> getAll() {
		return truyenTinhCamDao.findAll();
	}

	@Override
	public TruyenTinhCam findByProperty(String column, Object value) {
		List<TruyenTinhCam> lst = truyenTinhCamDao.findByProperty(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public List<TruyenTinhCam> getLstComicLinkImageNullBlogTruyen(int sourceBy){
		return truyenTinhCamDao.getLstComicLinkImageNullBlogTruyen(sourceBy);
	}

	@Override
	@Cacheable(cacheName="truyenTinhCamCacheDataController")
	public List<TruyenTinhCam> getAllLst(String kwSearch, String sort, String sortBy,	PagingInfo pInfo) {
		return truyenTinhCamDao.getAllLst(kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public void setHot(Long id) {
		try{
			TruyenTinhCam entity = findById(id);
			boolean isHot = entity.getIsHot();
			if(isHot){
				entity.setIsHot(false);
			}else{
				entity.setIsHot(true);
			}
			truyenTinhCamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void setIsDelete(Long id) {
		try{
			TruyenTinhCam entity = findById(id);
			boolean isDelete = entity.getIsDelete();
			if(isDelete){
				entity.setIsDelete(false);
			}else{
				entity.setIsDelete(true);
			}
			truyenTinhCamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	@Override
	@Cacheable(cacheName="lstTruyenTinhCamCacheTruyenController")
	public List<TruyenTinhCam> getListTruyenBy(String orderByColumn,int begin, int max) {
		return truyenTinhCamDao.getListTruyenBy(orderByColumn,begin,max);
	}

	@Override
	public List<TruyenTinhCam> getListTruyenRandom(int maxLimit) {
		return truyenTinhCamDao.getListTruyenRandomBy(maxLimit);
	}

	@Override
	public void updateTotalChapterr(Long id, float totalChapter) {
		try{
			TruyenTinhCam entity = findById(id);
			entity.setTotalChapter(totalChapter);
			truyenTinhCamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<TruyenTinhCam> getLstTruyenBySourceBy(int sourceBy) {
		return truyenTinhCamDao.getLstTruyenBySourceBy(sourceBy);
	}

	@Override
	public void updateViews(Long id, long views, long viewsWeek, long viewsMonth) {
		try{
			TruyenTinhCam entity = findById(id);
			entity.setViews(views);
			entity.setViewsWeek(viewsWeek);
			entity.setViewsMonth(viewsMonth);
			truyenTinhCamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="lstTruyenTinhCamTimKiemCacheTruyenController")
	public List<TruyenTinhCam> getLstByProperty(String column, Object value, int begin, int maxLimit) {
		return truyenTinhCamDao.getLstByProperty(column,value, begin, maxLimit);
	}

	@Override
	public List<Object> getObjectByNameUnsigned(String kwSearch, int max, String typeSearch) {
		return truyenTinhCamDao.getObjectByNameUnsigned(kwSearch,max,typeSearch);
	}

	@Override
	public List<TruyenTinhCam> getLstByPropertyWithPaging(String column, String value, String sortBy, PagingInfo pInfo) {
		return truyenTinhCamDao.getLstByPropertyWithPaging(column, value, sortBy, pInfo);
	}

	@Override
	public void updateLiked(Long idTruyen, long liked) {
		try{
			TruyenTinhCam entity = findById(idTruyen);
			entity.setLiked(liked);
			truyenTinhCamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void delete(Long id) {
		try{
			TruyenTinhCam entity = findById(id);
			List<ChapterTam> lstChapterTam = chapterTamService.getByTruyen(entity);
			if(lstChapterTam != null){
				int sizeList = lstChapterTam.size();
				for(int i=0;i<sizeList;i++){
					ChapterTam chapterTam = lstChapterTam.get(i);
					chapterTamService.delete(chapterTam.getId());
				}
			}
			List<ChapterTmpTruyenTinhCam> lstchapterTmpTruyenTinhCam = chapterTmpTruyenTinhCamService.getByTruyen(entity);
			if(lstchapterTmpTruyenTinhCam != null){
				int sizeList = lstchapterTmpTruyenTinhCam.size();
				for(int i=0;i<sizeList;i++){
					ChapterTmpTruyenTinhCam chapterTmpTruyenTinhCam = lstchapterTmpTruyenTinhCam.get(i);
					ChapterTruyenTinhCam chapterTruyenTinhCam = chapterTmpTruyenTinhCam.getChapterTruyenTinhCam();
					chapterTruyenTinhCamService.delete(chapterTruyenTinhCam.getId());
					chapterTmpTruyenTinhCamService.delete(chapterTmpTruyenTinhCam.getId());
				}
			}
			truyenTinhCamDao.delete(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void updateColumnString(long id, String column, String value) {
		try {
			TruyenTinhCam entity = findById(id);
			if(column.equals("linkImage")){
				entity.setLinkImage(value);
			}
			truyenTinhCamDao.merge(entity);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	@Cacheable(cacheName="lstTruyenTinhCamOrderByCacheTruyenController")
	public List<TruyenTinhCam> getLstByPropertyWithOrderByPaging(String orderBy, PagingInfo pInfo) {
		return truyenTinhCamDao.getLstByPropertyWithOrderByPaging(orderBy,pInfo);
	}

	@Override
	public List<TruyenTinhCam> getLstByTacGiaWithPaging(TacGia tacGia, String orderBy, PagingInfo pInfo) {
		return truyenTinhCamDao.getLstByTacGiaWithPaging(tacGia,orderBy,pInfo);
	}

	@Override
	public List<TruyenTinhCam> getLstByTacGiaLimit(TacGia tacGia, int begin, int maxLimit) {
		return truyenTinhCamDao.getLstByTacGiaLimit(tacGia, begin, maxLimit);
	}

	@Override
	public void update(Long idTruyen, String name, String alias,
			String linkImage, String nameImage, String info, TacGia tacGia,
			String categories, float totalChapter, boolean isDelete,
			boolean isFinished, boolean isHot, long liked, long views,
			long viewsWeek, long viewsMonth, int sourceBy,
			String linkSource, String translator) {
		try {
				TruyenTinhCam entity = findById(idTruyen);
				if(entity != null){
					entity.setName(name);
					entity.setAlias(alias);
					entity.setLinkImage(linkImage);
					entity.setNameImage(nameImage);
					entity.setInfo(info);
					entity.setTacGia(tacGia);
					entity.setCategories(categories);
					entity.setTotalChapter(totalChapter);
					entity.setIsDelete(isDelete);
					entity.setIsFinished(isFinished);
					entity.setIsHot(isHot);
					entity.setLiked(liked);
					entity.setViews(views);
					entity.setViewsWeek(viewsWeek);
					entity.setViewsMonth(viewsMonth);
					entity.setSourceBy(sourceBy);
					entity.setLinkSource(linkSource);
					entity.setTranslator(translator);
					truyenTinhCamDao.merge(entity);
				}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Async
	@Override
	public void updateViewsWeek() {
		List<TruyenTinhCam> lstTruyen = getAll();
		if(lstTruyen.size() > 0){
			int size = lstTruyen.size();
			for(int i=0; i < size; i++){
				TruyenTinhCam truyen = lstTruyen.get(i);
				truyen.setViewsWeek(0);
				truyenTinhCamDao.merge(truyen);
			}
		}
	}

	@Async
	@Override
	public void updateViewsMonth() {
		List<TruyenTinhCam> lstTruyen = getAll();
		if(lstTruyen.size() > 0){
			int size = lstTruyen.size();
			for(int i=0; i < size; i++){
				TruyenTinhCam truyen = lstTruyen.get(i);
				truyen.setViewsMonth(0);
				truyenTinhCamDao.merge(truyen);
			}
		}
	}

	@Override
	public List<TruyenTinhCam> findByPropertyNotNull(String property) {
		return truyenTinhCamDao.findByPropertyNotNull(property);
	}

}
